package com.zzld.modules.autoinn.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzld.core.service.BaseServiceImpl;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.Store;
import com.zzld.modules.autoinn.mapper.CustomerMapper;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.common.dto.TreeGrid;

@Service("customerService")
public class CustomerServiceImpl extends BaseServiceImpl<Customer> implements ICustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	

	
	public PageInfo selectpage(Map map,int pageNum,int pageSize){
		Map<String, Object> params = map;
		// 以name字段升序排序
		PageHelper.startPage(pageNum, pageSize);
		List<Customer> list = customerMapper.selpage(map);
		PageInfo page = new PageInfo(list);

		return page;
			
	}

	public Customer findCustomer(String wxaccount,String openid) throws Exception{
		Customer entity=new Customer();
		entity.setOpenid(openid);
		entity.setWxAccount(wxaccount);
		List<Customer> customer_list= customerMapper.select(entity);
		if(customer_list!=null && customer_list.size()>0){
			return customer_list.get(0);
		}
		return null;
	}
	
	public Customer findCustomerByPhone(String wxaccount,String phone) throws Exception{
		Customer entity=new Customer();
		entity.setWxAccount(wxaccount);
		entity.setPhoneNum(phone);
		List<Customer> customer_list= customerMapper.select(entity);
		if(customer_list!=null && customer_list.size()>0){
			return customer_list.get(0);
		}
		return null;
	}
	
	public List<Customer> selall(){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sort", "application_time");
		map.put("order", "desc");
		List<Customer> list = customerMapper.selpage(map);
		return list;
	}
	
	public Customer selectbysurplus(String id){
		return customerMapper.selectbysurplus(id);
	}
	
   public Integer updatesurplus(Map<String,Object> map){
	    return customerMapper.updatesurplus(map);
   }
   
   public List<TreeGrid> selcustomer(){
	   List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		Example e = new Example(Customer.class);
		List<Customer> listRes = customerMapper.selectByExample(e);
		if (listRes != null && listRes.size() > 0) {
			for (Customer res : listRes) {
				TreeGrid treeGrid = new TreeGrid();
				BeanUtils.copyProperties(res, treeGrid);
				treeGrid.setId(res.getCustomerId());
				treeGrid.setText(res.getNickname());
				treeGrids.add(treeGrid);
			}
		}
		return treeGrids;
   }
	
}
